package com.neusoft.neu24.his.hisapp.controller;

import com.neusoft.neu24.his.hisapp.entity.Patient;
import com.neusoft.neu24.his.hisapp.service.IPatientService;
import com.neusoft.neu24.his.hisapp.service.impl.PatientServiceImpl;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

;import java.util.List;

public class ListViewController {
    private Stage myStage;

    public void setMyStage(Stage myStage) {
        this.myStage = myStage;
    }

    //    定义Result控制器对象
    private ResultViewController resultViewController;
// setter
    public void setResultViewController(ResultViewController resultViewController) {
        this.resultViewController = resultViewController;
    }

    private StringProperty tf_searchProperty = new SimpleStringProperty("");

    private IPatientService patientService =new PatientServiceImpl();

    @FXML
    TableColumn<Patient,String> nameCloumn ;
    @FXML
    TableColumn<Patient,String>  ageCloumn ;
    @FXML
    TableColumn<Patient,String>  sexCloumn ;
    @FXML
    TableColumn<Patient,String>  statusCloumn ;

    @FXML
    TextField  tf_search;

    @FXML
    TableView<Patient> tv_patients;
    @FXML
    public void initialize(){
        nameCloumn.setCellValueFactory( new PropertyValueFactory("name"));
        ageCloumn.setCellValueFactory( new PropertyValueFactory("age"));
        sexCloumn.setCellValueFactory( new PropertyValueFactory("sex"));
        statusCloumn.setCellValueFactory( new PropertyValueFactory("status"));

        tv_patients.getItems().addAll(  patientService.selectAllPatient() );
        System.out.println("ListViewController.initialize");

        tf_search.textProperty().bindBidirectional(tf_searchProperty);
//        为了获得绑定属性的变化，需要添加事件

        tf_searchProperty.addListener((observable, oldValue, newValue) -> {
            List<Patient> lps =  patientService.selectPatientByAge(newValue);
            if(lps!=null){
//                删除表格中的旧数据
                tv_patients.getItems().clear();
//                填充新数据
                tv_patients.getItems().addAll(lps);
            }

        });
// 定义表格的键盘事件


    }


    @FXML
    public void tf_searchKeyReleased(KeyEvent event) {
        if( event.getCode().equals(KeyCode.DOWN)){
//            表格获得焦点
            tv_patients.requestFocus();
//            第一行被选中
            tv_patients.getSelectionModel().selectFirst();
        }
        if (event.getCode().equals(KeyCode.UP)){
            if (tv_patients.getSelectionModel().getSelectedIndex()==0){
                tf_search.requestFocus();
                tf_search.selectAll();
            }
        }
    }


    @FXML
    public void tv_PatientsKeyReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.ENTER)){
            Patient patient =  tv_patients.getSelectionModel().getSelectedItem();
            resultViewController.getTv_patients().getItems().add(patient);
//            this.myStage.close();

        }
    }
}
